/*
 * $Id: RequestDefnLookup.java,v 1.9 2009/08/06 15:39:14 afaichun Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.util;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.database.dao.IRequestDefnDAO;
import com.ge.healthcare.autosc.common.database.dao.IRequestTypeDAO;
import com.ge.healthcare.autosc.common.database.pojo.CfgRequestDefn;
import com.ge.healthcare.autosc.common.database.pojo.CfgRequestType;

/**
 * 
 * @author 212042946
 * 
 */
public class RequestDefnLookup {

	private static final String DEFAULT_MODEL_TYPE = "*";
	private static IRequestDefnDAO requestDefnDAO = null;
	private static IRequestTypeDAO requestTypeDAO = null;

	/**
	 * 
	 * @param tranType
	 * @param modality
	 * @param modelType
	 * @param serviceId
	 * @param process
	 * @return
	 */
	public static CfgRequestDefn lookup(String tranType, String modality,
			String modelType, String serviceId, String process) {
		String methodName = "lookup";
		String requestTypeName = buildRequestTypeName(tranType, serviceId,
				process);
		CfgRequestType requestType = requestTypeDAO
				.getRequestTypeByName(requestTypeName);
		if (requestType == null) {
			// Could not find by specific serviceId and process
			// Use default and give one more shot
			ASCLogger.info(RequestDefnLookup.class, methodName,
					"Cannot find request type with request type name "
							+ requestTypeName
							+ ". Try to use default process/createdBy.");
			process = "DEFAULT";
			requestTypeName = buildRequestTypeName(tranType, serviceId, process);
			ASCLogger.info("Try default request type " + requestTypeName);
			
			requestType = requestTypeDAO.getRequestTypeByName(requestTypeName);
			if (requestType == null) {
				// configuration issue, no hope.
				String errMsg = new StringBuffer()
						.append(
								"Still cannot find request type with request type name ")
						.append(requestTypeName).append("\n").append(
								"{modality: ").append(modality).append("},")
						.append("{modelType: ").append(modelType).append("},")
						.append("{serviceId: ").append(serviceId).append("},")
						.append("{process/createdby: ").append(process).append(
								"}").append("\n").toString();
				ASCLogger.error(RequestDefnLookup.class, methodName, errMsg);
			}
		}
		return RequestDefnLookup.lookup(requestType, modality, modelType);
	}

	/**
	 * @param requestTypeId
	 * @param modality
	 * @param modelType
	 * @return
	 */
	public static CfgRequestDefn lookup(CfgRequestType requestType, String modality,
			String modelType) {
		String methodName = "lookup";
		CfgRequestDefn requestDefn = null;
		if(requestType != null) {
			Long requestTypeId = requestType.getRequestTypeId();
			requestDefn = requestDefnDAO.getByModalityModelTypeAndRequestTypeId(
					modality, modelType, requestTypeId);
			if (requestDefn == null) {
				StringBuffer msg = new StringBuffer();
				msg.append("Checking Default Model Type");
				ASCLogger.info(msg.toString());
				requestDefn = requestDefnDAO.getByModalityModelTypeAndRequestTypeId(
						modality, DEFAULT_MODEL_TYPE, requestTypeId);
			}
			if (requestDefn == null) {
				String errMsg = new StringBuffer().append(
						"Cannot find request definition").append("\n").append(
						"{requestTypeId: ").append(requestTypeId).append("},")
						.append("{modality: ").append(modality).append("},")
						.append("{modelType: ").append(modelType).append("}\n")
						.toString();
				ASCLogger.error(RequestDefnLookup.class, methodName, errMsg);
			}
		}
		return requestDefn;
	}

	/**
	 * Set RequestDefnDAO
	 * 
	 * @param requestDefnDAO
	 */
	public void setRequestDefnDAO(IRequestDefnDAO requestDefnDAO) {
		RequestDefnLookup.requestDefnDAO = requestDefnDAO;
	}

	/**
	 * Set RequestTypeDAO
	 */
	public void setRequestTypeDAO(IRequestTypeDAO requestTypeDAO) {
		RequestDefnLookup.requestTypeDAO = requestTypeDAO;
	}

	private static String buildRequestTypeName(String prefix,
			String serviceId, String process) {
		String sep = ".";
		if (serviceId != null) {
			serviceId = sep + serviceId.toUpperCase().trim();
		} else {
			serviceId = "";
		}

		if (process != null) {
			process = sep + process.toUpperCase().trim();
		} else {
			process = "";
		}
		return (prefix + serviceId + process);
	}
}
